import cv2
import numpy as np

def main():
    video_path = 'shaoshuai.mp4'
    cap = cv2.VideoCapture(video_path)

    if not cap.isOpened():
        print("Error: Could not open video.")
        return

    fps = cap.get(cv2.CAP_PROP_FPS)
    delay = int(1000 / fps)
    original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    # 原视频大小比例
    scale_factor = 1.8

    # 定义一些特效参数
    text = "   "
    font = cv2.FONT_HERSHEY_SIMPLEX
    font_scale = 1
    font_color = (int(255 * 0.2), int(255 * 0.2), int(255 * 0.2))  # 调整后的白色，模拟20%不透明度
    line_type = 2

    while True:
        ret, frame = cap.read()
        if not ret:
            break

            edge_threshold1 = 80
            edge_threshold2 = 200
            # 应用Canny边缘检测
            edges = cv2.Canny(blurred_frame, edge_threshold1, edge_threshold2)

            # 膨胀操作，使边缘变粗
            kernel_dilation = np.ones((3, 3), np.uint8)
            dilated_edges = cv2.dilate(edges, kernel_dilation, iterations=dilation_iterations)

        # 转换为灰度图
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # 应用高斯模糊，减少噪声对轮廓的影响
        blurred_frame = cv2.GaussianBlur(gray_frame, (9, 9), 0)

        # 应用自适应阈值处理，增强轮廓
        thresh_frame = cv2.adaptiveThreshold(blurred_frame, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 4)

        new_width = int(original_width * scale_factor)
        new_height = int(original_height * scale_factor)
        resized_frame = cv2.resize(thresh_frame, (new_width, new_height))

        # 将图像反转，得到类似素描的效果（黑色线条，白色背景）
        inverted_frame = 255 - resized_frame

        # 在视频帧上添加文字
        cv2.putText(inverted_frame, text, (50, 50), font, font_scale, font_color, line_type)

        cv2.imshow('Video', inverted_frame)
        if cv2.waitKey(delay) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()
